package org.jwcloud.project.module.zx.project.controller;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.db.domain.RtaProjectInfo;
import org.jwcloud.project.module.zx.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle", description = "项目基本信息管理", tags = "project")
@RestController
@RequestMapping("/rest/project")
public class ProjectController extends CrudController {

	@Autowired
	private ProjectService projectService;

	@PostMapping("doProjectInsert")
	@ApiOperation(value = "添加项目基本信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "projectName", value = "项目名称 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "projectType", value = "项目类型 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "relTemplateId", value = "模板id", required = true, paramType = "query"),
			@ApiImplicitParam(name = "startTime", value = "启动时间", required = true, paramType = "query") })
	public Resp<Response<?>> doProjectInsert(HttpServletRequest request, @RequestBody RtaProjectInfo rtaProjectInfo) {

		Timestamp d = new Timestamp(System.currentTimeMillis());
		
		rtaProjectInfo.setCreatePerson(request.getHeader("_session_user"));
		rtaProjectInfo.setCreateDept(request.getHeader("_session_org"));
		rtaProjectInfo.setCreateOrg(request.getHeader("_session_org"));
		rtaProjectInfo.setCreateDate(new Date(d.getTime()));		
		rtaProjectInfo.setUpdatePerson(request.getHeader("_session_user"));
		rtaProjectInfo.setUpdateDept(request.getHeader("_session_org"));
		rtaProjectInfo.setUpdateOrg(request.getHeader("_session_org"));
		rtaProjectInfo.setUpdateDate(new Date(d.getTime()));
		
		projectService.doProjectInsert(rtaProjectInfo);
		Response<?> response = new Response();
		response.setCode(0);
		// response.setPayload(map);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@PostMapping("doProjectUpdate")
	@ApiOperation(value = "修改项目基本信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "projectName", value = "项目名称 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "projectType", value = "项目类型 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "relTemplateId", value = "模板id", required = true, paramType = "query"),
			@ApiImplicitParam(name = "startTime", value = "启动时间", required = true, paramType = "query") })
	public Resp<Response<?>> doProjectUpdate(HttpServletRequest request,@RequestBody RtaProjectInfo rtaProjectInfo) {
		
		Timestamp d = new Timestamp(System.currentTimeMillis());
		rtaProjectInfo.setUpdatePerson(request.getHeader("_session_user"));
		rtaProjectInfo.setUpdateDept(request.getHeader("_session_org"));
		rtaProjectInfo.setUpdateOrg(request.getHeader("_session_org"));
		rtaProjectInfo.setUpdateDate(new Date(d.getTime()));
		
		projectService.doProjectUpdate(rtaProjectInfo);
		Response<?> response = new Response();
		response.setCode(0);
		// response.setPayload(map);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@DeleteMapping("doProjectDel")
	@ApiOperation(value = "删除项目基本信息", notes = "")
	@ApiImplicitParams({})
	public Resp<Response<?>> doProjectDel(HttpServletRequest request,  @RequestParam Integer id) {

//		Timestamp d = new Timestamp(System.currentTimeMillis());
//		rtaProjectInfo.setUpdatePerson(request.getHeader("_session_user"));
//		rtaProjectInfo.setUpdateDept(request.getHeader("_session_org"));
//		rtaProjectInfo.setUpdateOrg(request.getHeader("_session_org"));
//		rtaProjectInfo.setUpdateDate(new Date(d.getTime()));
		
		projectService.doProjectDel(id);
		Response<?> response = new Response();
		response.setCode(0);
		// response.setPayload(map);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}
	@PostMapping("doProjectCommit")
	@ApiOperation(value = "项目归档", notes = "")
	@ApiImplicitParams({})
	public Resp<Response<?>> doProjectCommit(HttpServletRequest request,  @RequestParam Integer id) {

//		Timestamp d = new Timestamp(System.currentTimeMillis());
//		rtaProjectInfo.setUpdatePerson(request.getHeader("_session_user"));
//		rtaProjectInfo.setUpdateDept(request.getHeader("_session_org"));
//		rtaProjectInfo.setUpdateOrg(request.getHeader("_session_org"));
//		rtaProjectInfo.setUpdateDate(new Date(d.getTime()));
		
 		projectService.doProjectCommit(id);
		Response<?> response = new Response();
		response.setCode(0);
		// response.setPayload(map);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}
	@GetMapping("doProjectList")
	@ApiOperation(value = "项目基本信息列表", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "currentPage", value = "分页参数 ", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "rtaProjectInfo", value = "查询条件 ", required = false, paramType = "query") })
	public Resp<Response<?>> doProjectList(Pagination pagination ,
			@RequestParam(required = false) Map params) {
		
		ListBounds listBounds = getBounds(pagination);
		ListSlice<RtaProjectInfo> list = projectService.doProjectList(listBounds, params);
		Response<ListSlice<RtaProjectInfo>> response = new Response<ListSlice<RtaProjectInfo>>();
		response.setCode(0);
		response.setPayload(list);
		// return ResponseEntity.ok(response);

		return Resp.success(response);
	}

	@Override
	public CrudService getService() {
		// TODO Auto-generated method stub
		return projectService;
	}

}
